<?php 
include './DataJson.class.php';
include './functions.php';
$loginData=$_POST;
$loginData['account']=trim($loginData['account']);
$loginData['password']=trim($loginData['password']);

if (empty($loginData['account'])) {
	$error=0;
	
}elseif (empty($loginData['password'])) {
	$error=2;
}
$loginData['password']=md5($loginData['password']);
// var_dump($loginData);exit;
$userExist=false;

$data=DataJson::readJson('./Db/user.json');
// var_dump($data);exit;
foreach ($data as $key => $userInfo) {
	if (in_array($loginData['account'],$userInfo)) {
		if ($loginData['password']==$userInfo['password']) {
			session_start();
			$_SESSION["username"]=$userInfo['username'];
			$_SESSION["loginSuccess"]=true;
			header("Location:./index.php");
		}else{
			$error=3;//帐号和密码不对
		}
		$userExist=true;
		break;
	}
}

if (!$userExist) {
	$error=5;
}

 ?>

 <!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<script type="text/javascript">
		var  error = <?php echo $error ?>;
		switch(error){
			case 0:
				if(confirm("帐号不能为空,请重新输入")){
					location.href = "./index.php";
				}break;
			// case 1:
			// 	if(confirm("邮箱不能为空")){
			// 		location.href = "./index.php";
			// 	}break;
			case 2:
				if(confirm("密码不能为空")){
					location.href = "./index.php";
				}break;
			case 3:
				if(confirm("你输入的帐号和密码不对")){
					location.href = "./index.php";
				}break;
			case 4:
				if(confirm("输入的用户名不合法")){
					location.href = "./index.php";
				}break;
			case 5:
				if(confirm("你输入的帐号不存在")){
					location.href = "./index.php";
				}break;
			// case 6:
			// 	if(confirm("邮箱已存在")){
			// 		location.href = "./index.php";
			// 	}break;
			// case 100:
			// 	if(confirm("注册成功!")){
			// 		location.href = "./index.php";
			// 	}break;
		}
		
	</script>
</head>
<body>
	
</body>
</html>